import { _decorator, Camera, Component, log, Node, tween, Tween, v2, v3, Vec2, Vec3 } from 'cc';
import { ZTool } from '../AshFramework/Utils/ZTool';
const { ccclass, property } = _decorator;

const v2_0: Vec3 = v3();
const v2_1: Vec3 = v3();
const v2_2: Vec3 = v3();

@ccclass('CameraController')
export class CameraController extends Component {

    static Instance: CameraController = null;

    @property(Node)
    target: Node = null;

    @property(Vec3)
    offset: Vec3 = v3(0, 0);

    @property
    BorderX: number = 0;

    camera: Camera = null;
    stopFollow: boolean = false;
    isPause: boolean = false;

    onLoad() {
        CameraController.Instance = this;
        this.camera = this.node.getComponent(Camera);
    }

    // Move(delta: Vec2) {
    //     this.stopFollow = true;
    //     this.node.getPosition(v2_3);
    //     v2_3.add3f(delta.negate().multiplyScalar(5))
    //     v2_3.set(cc.v2(ZTool.Clamp(v2_3.x, -5000, 5000), ZTool.Clamp(v2_3.y, -5000, 5000)));
    //     this.node.setPosition(v2_3);
    // }

    // ZoomIn() {
    //     Tween.stopAllByTarget(this.camera);
    //     tween(this.camera).to(0.2, { zoomRatio: 1 }, { easing: EasingType.quadIn }).start();
    // }

    // ZoomOut(ratio: number = 0.5) {
    //     Tween.stopAllByTarget(this.camera);
    //     tween(this.camera).to(0.2, { zoomRatio: ratio }, { easing: EasingType.quadIn }).start();
    // }

    update(dt) {
        if (this.stopFollow) return;
        if (!this.target) return;
        if (this.target && !this.target.active) return;
        if (this.isPause) return;

        this.node.getWorldPosition(v2_0);
        this.target.getWorldPosition(v2_1);
        v2_1.set(v2_1.x, v2_1.y, this.node.worldPosition.z);
        v2_1.add(this.offset);
        Vec3.lerp(v2_2, v2_0, v2_1, 0.5);
        this.node.setWorldPosition(v2_2);
        //**判断摄像机有没有超出边界 */
        if (this.node.position.x > this.BorderX) {
            this.node.position = v3(this.BorderX, this.node.position.y, this.node.position.z);
        }
    }

}



